package com.sjq.yqgl.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.FTPClientHelper;
import com.sjq.common.util.ImportsExcel;
import com.sjq.common.util.PageResults;
import com.sjq.common.util.PrimaryKeyUtil;
import com.sjq.yqgl.dao.YqbsDao;
import com.sjq.yqgl.dao.YqfjDao;
import com.sjq.yqgl.dao.YqmqjczDao;
import com.sjq.yqgl.dao.YqxfDao;
import com.sjq.yqgl.entity.Yqbs;
import com.sjq.yqgl.entity.Yqfj;
import com.sjq.yqgl.entity.Yqmqjcz;
import com.sjq.yqgl.entity.Yqxf;
import com.sjq.yqgl.service.YqfjService;

@Service
public class YqfjServiceImpl implements YqfjService{
	@Resource
	private YqfjDao dao;
	@Resource
	private YqbsDao bsDao;
	@Resource
	private YqxfDao xfDao;
	@Resource
	private YqmqjczDao mqDao;
	@Resource
	private YqmqjczDao yqmqjczDao;
	/**新增  **/
	@Override
	public JSONObject addYqfj(Yqfj yqfj,LoginAccount loginAccount) {
		/**标准注册登记人信息***/
		/*Result<Yqfj> result=new Result<Yqfj>();
		yqfj.setDjdw_gajgjgdm(loginAccount.getUserDwbm());
		yqfj.setDjdw_dwmc(loginAccount.getUserDwmc());
		yqfj.setDjr_gmsfhm(loginAccount.getUserId());
		yqfj.setDjr_xm(loginAccount.getUserName());
		*/
		int count=dao.addYqfj(yqfj);
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
			//蓝信注册通知
		
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	/**新增含有附件的文档上传至FTP***/
	@Override
	public JSONObject addYqfjFilesByFTP(Yqfj yqfj, CommonsMultipartFile[]  fj,LoginAccount loginAccount) {
		yqfj.setDjdw(loginAccount.getUserDwbm());
		yqfj.setDjdwmc(loginAccount.getUserDwmc());
		yqfj.setDjrjh(loginAccount.getAccountId());
		
		String yqfjId=	PrimaryKeyUtil.getUUID();
		yqfj.setXxzjbh(yqfjId);
		JSONObject json =new JSONObject();
		json.put("code","200");
		json.put("msg","ok");
		String remotePath="\\"+loginAccount.getUserId()+"\\";//远程登录默认同步至当前身份证号码文件夹下面
		FTPClientHelper ftp=new FTPClientHelper(remotePath);
		for(int i=0;i<fj.length;i++){
			String originalFileName=fj[i].getOriginalFilename();//获取原文件名称
			String suffix=	originalFileName.substring(originalFileName.lastIndexOf("."));//获取源文件类型
			String newFileName=PrimaryKeyUtil.getUUID()+""+suffix;	//将文件重新命名
			boolean flage=false;
			try {
				flage = ftp.upload(fj[i].getInputStream(), newFileName);
			} catch (IOException e) {
				e.printStackTrace();
			}//是否上传成功
			if(flage){
				/***上传成功，将信息插入数据库***/
				String url=remotePath+newFileName;//
				try {
					List<List<Object>> list=	ImportsExcel.readExcel(fj[i]);
					List<Object> title=list.get(0);//头
					List<Object> qfxx=list.get(1);//签发信息
					List<Object> gl=list.get(2);//确定登记
					String fjlx=yqfj.getFjlx();
					ArrayList <Yqbs> yqbs_nrs =new ArrayList<Yqbs>();
					ArrayList <Yqxf> yqxf_nrs =new ArrayList<Yqxf>();
					ArrayList <Yqmqjcz> yqmqjcz_nrs =new ArrayList<Yqmqjcz>();
					int number=0;
					//int 
					for(int x=3;x<list.size();x++){
						List<Object> data=list.get(x);
						if(fjlx.equals("1")){
//							/信息上报
							Yqbs yqbs=new Yqbs();
							yqbs.setXxzjbh(PrimaryKeyUtil.getUUID());
							yqbs.setXh((String)data.get(0));
							yqbs.setXm((String)data.get(1));
							yqbs.setSjhm((String)data.get(2));
							yqbs.setSfhm((String)data.get(3));
							yqbs.setHjdz((String)data.get(4));
							yqbs.setXzjd((String)data.get(5));
							String hcfs=(String)data.get(6);
							try{
							String hssj=(String)data.get(7);
								Integer.parseInt(hssj);
								
							}catch(Exception e){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，回宿时间填写错误！请按标准20200130格式填写");
								return json;
								
							}String dqwz=(String)data.get(8);
							String stzk=(String)data.get(9);
							String fhcs=(String)data.get(10);
							String xm=(String)data.get(1);
							
							if(xm.trim().equals("")){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，姓名为空！");
								return json;
							}
							if(!hcfs.equals("电话") && !hcfs.equals("见面")){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，核查方式填写错误！请按标准填写");
								return json;
							}
														
							if(dqwz.equals("是")){
								if(!stzk.equals("发热") && !stzk.equals("咳嗽") && !stzk.equals("其它呼吸道症状") && !stzk.equals("无")){
									json.put("code","300");
									json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，有无身体不适填写错误！请按标准填写 ");
									return json;
								}
								
								if(!fhcs.equals("居家观察") && !fhcs.equals("医学隔离") ){
									json.put("code","300");
									json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，防护措施填写错误！请按标准填写 ");
									return json;
								}
								
							}else if(dqwz.equals("否")){
								
							}else {
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，是否在宿填写错误！请按标准填写 ");
								return json;
							}
							yqbs.setHcfs((String)data.get(6));
							yqbs.setHssj((String)data.get(7));
							yqbs.setDqwz((String)data.get(8));
							String Dqwz=(String)data.get(8);
							yqbs.setStzk((String)data.get(9));
							yqbs.setFhcs((String)data.get(10));
							yqbs.setSqbbry((String)data.get(11));
							yqbs.setLxdh((String)data.get(12));
							yqbs.setSqmj((String)data.get(13));
							yqbs.setSqmjlxdh((String)data.get(14));
							yqbs.setGlrwid(yqfjId);
							yqbs.setDjdw(loginAccount.getUserDwbm());
							yqbs.setDjdwmc(loginAccount.getUserDwmc());
							yqbs.setDjrjh(loginAccount.getAccountId());
							yqbs_nrs.add(yqbs);
							//bsDao.addYqbs(yqbs);
						//	number++;
							
						}else if(fjlx.equals("0")){
							//数据下发
							Yqxf yqbs=new Yqxf();
							yqbs.setXxzjbh(PrimaryKeyUtil.getUUID());
							yqbs.setXh((String)data.get(0));
							yqbs.setXm((String)data.get(1));
							yqbs.setSjhm((String)data.get(2));
							yqbs.setSfhm((String)data.get(3));
							yqbs.setXfj((String)data.get(4));
							String xfj=(String)data.get(4);
							if(!xfj.equals("宿城") && !xfj.equals("宿豫") && !xfj.equals("沭阳") && !xfj.equals("泗洪") && !xfj.equals("泗阳") && !xfj.equals("湖滨") && !xfj.equals("经开") && !xfj.equals("苏宿")&& !xfj.equals("洋河") ){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，下发分县局填写错误！请按标准填写 ");
								return json;
							}
							
							String sjly=(String)data.get(7);
							if(!sjly.equals("省厅下发") && !sjly.equals("技侦下发") && !sjly.equals("网安下发") && !sjly.equals("过车轨迹") && !sjly.equals("旅馆住宿") && !sjly.equals("网吧上网") && !sjly.equals("其他") ){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，填写数据来源错误！请按标准填写 ");
								return json;
							}
							yqbs.setPcs((String)data.get(5));
							yqbs.setDz((String)data.get(6));
							yqbs.setSjly((String)data.get(7));
							yqbs.setSjgz((String)data.get(8));
							yqbs.setXfsj((String)data.get(9));
							yqbs.setHdgj((String)data.get(10));
							yqbs.setDjdw(loginAccount.getUserDwbm());
							yqbs.setDjdwmc(loginAccount.getUserDwmc());
							yqbs.setDjrjh(loginAccount.getAccountId());
							yqbs.setGlrwid(yqfjId);
							yqxf_nrs.add(yqbs);
							//xfDao.addYqxf(yqbs);
						}else if(fjlx.equals("2")){
							//密切接触者数据
							Yqmqjcz yqbs=new Yqmqjcz();
							yqbs.setXxzjbh(PrimaryKeyUtil.getUUID());
							yqbs.setXh((String)data.get(1));
							
							yqbs.setXm((String)data.get(2));
							yqbs.setSfhm((String)data.get(3));
							yqbs.setHjdz((String)data.get(4));
							yqbs.setXzz((String)data.get(5));
							yqbs.setSjhm((String)data.get(6));
							yqbs.setJcsj((String)data.get(7));
							yqbs.setYhzgx((String)data.get(8));
							yqbs.setSfzs((String)data.get(9));
							yqbs.setStzk((String)data.get(10));
							yqbs.setFhcs((String)data.get(11));
							yqbs.setSqbbr((String)data.get(12));
							yqbs.setLxdh((String)data.get(13));
							yqbs.setSqmj((String)data.get(14));
							yqbs.setSqmjdh((String)data.get(15));
							yqbs.setDjbgbs("1");
							yqbs.setDjdw(loginAccount.getUserDwbm());
							yqbs.setDjdwmc(loginAccount.getUserDwmc());
							yqbs.setDjrjh(loginAccount.getAccountId());
							yqbs.setGlrwid(yqfjId);
							String xm= (String)data.get(2);
							String dqwz=(String)data.get(9);
							String stzk=(String)data.get(10);
							String fhcs=(String)data.get(11);
							String yhzgx=(String)data.get(8);
							if(!yhzgx.equals("无") && !yhzgx.equals("家人")&& !yhzgx.equals("同事")&& !yhzgx.equals("朋友")){
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，与患者关系填写错误！请按标准填写 ");
								return json;
								
							}
							if(dqwz.equals("是")){
								if(!stzk.equals("发热") && !stzk.equals("咳嗽") && !stzk.equals("其它呼吸道症状") && !stzk.equals("无")){
									json.put("code","300");
									json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，有无身体不适填写错误！请按标准填写 ");
									return json;
								}
								
								if(!fhcs.equals("居家观察") && !fhcs.equals("医学隔离") ){
									json.put("code","300");
									json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，防护措施填写错误！请按标准填写 ");
									return json;
								}
								
							}else if(dqwz.equals("否")){
								
							}else {
								json.put("code","300");
								json.put("msg","异常：第"+x+" 行  !"+(String)data.get(1)+"，是否在宿填写错误！请按标准填写 ");
								return json;
							}
									yqmqjcz_nrs.add(yqbs);
							
						}
					}
					
					if(fjlx.equals("0")){
						yqfj.setTbdw((String)qfxx.get(1));
						yqfj.setTbsj((String)qfxx.get(3));
						yqfj.setRbr((String)qfxx.get(5));
						yqfj.setShr((String)qfxx.get(7));
						for(int j=0;j<yqxf_nrs.size();j++){
							Yqxf yqbs=yqxf_nrs.get(j);
							xfDao.addYqxf(yqbs);
						}
					}else if(fjlx.equals("1")){
						yqfj.setTbdw((String)qfxx.get(1));
						yqfj.setTbsj((String)qfxx.get(3));
						yqfj.setRbr((String)qfxx.get(5));
						yqfj.setShr((String)qfxx.get(7));
						for(int j=0;j<yqbs_nrs.size();j++){
							Yqbs yqbs=yqbs_nrs.get(j);
							bsDao.addYqbs(yqbs);
						}
					}else if(fjlx.equals("2")){
						yqfj.setTbdw((String)qfxx.get(2));
						yqfj.setTbsj((String)qfxx.get(4));
						yqfj.setRbr((String)qfxx.get(6));
						yqfj.setShr((String)qfxx.get(8));
						for(int j=0;j<yqmqjcz_nrs.size();j++){
							Yqmqjcz yqbs=yqmqjcz_nrs.get(j);
							yqmqjczDao.addYqmqjcz(yqbs);
						}
						yqfj.setQzrymc(gl.get(0).toString());
					}
					yqfj.setFjlj(url);
					yqfj.setTitle((String)title.get(0));
					yqfj.setOfjmc(originalFileName);
					yqfj.setFjmc(newFileName);
					 dao.addYqfj(yqfj);
					
				} catch (Exception e) {
					
					e.printStackTrace();
					json.put("code","300");
					json.put("msg","解析异常");
				}
				
			}else{
				json.put("code","300");
				json.put("msg","error");
			}
		}
		
		return json;
	}

	/**删除 **/
	@Override
	public JSONObject deleteYqfj(String yqfjId,String fjlx,LoginAccount loginAccount) {
		if(fjlx.equals("0")){
			//下发  删除
			xfDao.deleteYqxf(yqfjId);
		}else if(fjlx.equals("1")){
			bsDao.deleteYqbs(yqfjId);
		}else if(fjlx.equals("2")){
			mqDao.deleteYqmqjcz(yqfjId);
		}
		int count=dao.deleteYqfj(yqfjId);
		JSONObject json =new JSONObject();
		if(count>0){
			json.put("code","200");
			json.put("msg","ok");
		}else{
			json.put("code","300");
			json.put("msg","error");
		}
		return json;
	}
	
	
	
	
	/**更新 **/
	@Override
	public JSONObject updateYqfj(Yqfj yqfj,LoginAccount loginAccount) {
		//跟新人信息
		/*
		yqfj.setXgr_gajgjgdm(loginAccount.getUserDwbm());
		yqfj.setXgr_dwmc(loginAccount.getUserDwmc());
		yqfj.setXgr_gmsfhm(loginAccount.getUserId());
		yqfj.setXgr_xm(loginAccount.getUserName());
		*/
		JSONObject result=new JSONObject();
		int count=dao.updateYqfj(yqfj);
		if(count>0){
			result.put("msg", "success");
			result.put("code","200");
		}else{
			result.put("msg", "error");
			result.put("code","300");
		}
		return result;
	}
	
	/**查询**/
	@Override
	public PageResults<?> findYqfjByPage(int pageNo, int pageSize, Yqfj yqfj) {
		PageResults<?> pageResults=dao.selectYqfjByPage(pageNo, pageSize, yqfj);
		return pageResults;
	}
	
	/**根据查询条件查询所有的数据**/
	@Override
	public JSONObject findYqfjByListAll(Yqfj yqfj,LoginAccount la){
		List<Map<String, String>> list=dao.findYqfjByListAll(yqfj);
		/**
		* 权限校验
		**/
		JSONObject result=new JSONObject();
		result.put("msg", "success");
		result.put("code","200");
		result.put("data",list);
		return result;
	}
	/*****/
	
}